package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class LabelService {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;


    public List<Label> findAll(){
        return labelDao.findAll();

    }
    public List<Label> findBydate(Date date){
        List<Label> byDate = labelDao.findByDate(date);
        System.out.println(byDate);
        return byDate;
    }

    public Label findById(String id){
        return labelDao.findById(id).get();
    }

    public void save(Label label){
        label.setId(idWorker.nextId()+"");
        label.setDate(new Date(new java.util.Date().getTime()));
        labelDao.save(label);
    }

    public void update(Label label){
        labelDao.save(label);
    }

    public void delete(String id){
        labelDao.deleteById(id);
    }

    public List<Label> findSearch(Label label) {
        List<Predicate> predicates=new ArrayList<>();
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param root    根对象,也就是把条件封装到那个对象,where 类名 =label.getid
             * @param criteriaQuery 封装都是查询关键字,比如group by ourder by
             * @param criteriaBuilder cb 用来封装天剑对象,如果直接返回null.表示不需要任何条件
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if(StringUtils.isNotBlank(label.getLabelname())){
                    Predicate p = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    predicates.add(p);
                }
                if(StringUtils.isNotBlank(label.getState())){
                    Predicate p = criteriaBuilder.equal(root.get("state").as(String.class), label.getState());
                    predicates.add(p);
                }
                Predicate[] predicates1=new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        });

    }

    public Page<Label> pageQuery(Label label, int page, int size) {
        List<Predicate> predicates=new ArrayList<>();
        Pageable pageable= PageRequest.of(page-1,size);
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param root    根对象,也就是把条件封装到那个对象,where 类名 =label.getid
             * @param criteriaQuery 封装都是查询关键字,比如group by ourder by
             * @param criteriaBuilder cb 用来封装天剑对象,如果直接返回null.表示不需要任何条件
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if(StringUtils.isNotBlank(label.getLabelname())){
                    Predicate p = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    predicates.add(p);
                }
                if(StringUtils.isNotBlank(label.getState())){
                    Predicate p = criteriaBuilder.equal(root.get("state").as(String.class), label.getState());
                    predicates.add(p);
                }
                Predicate[] predicates1=new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        },pageable);
    }
}
